//index.js
//获取应用实例
const app = getApp()
let canvas = require('../../common/js/canvas.js');
let Axios = require('../../common/js/ajax.js'); //引入Axios方法
let Compress = require('../../common/js/compressImage.js'); //引入传图方法
let WxValidate = require('../../common/js/WxValidate.js'); //表单验证
let Mtils = require('../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
var page = 1;
var switch_p = true
Page({
	data: {
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 2, //当前导航
		cur_id: 1,
		priceTk: false,
		cur_price: 0,
		cur_pId: 0,
		culum: 1,
		cancelTk: false,
		tips: false,
		cancelTap: true,
		cancelRepTk: false,
		SendTk: false,
		express: [],
		expressId: [],
		selectExp: '',
		index: 0,
		expressNum: '',
		orders: [],
		store_id: 0,
		agreeTk: false,
		Reason: '',
		isIpx:app.globalData.isIpx?true:false
	},
	//事件处理函数
	onLoad: function(e) {
		this.initValidate()
		this.load = this.selectComponent("#loading");
		this.setData({
			store_id: e.id,
		})
		if (e.culum) {
			this.setData({
				culum: e.culum,
				cur_id: e.culum
			})
		}
		var _this = this
		if (app.globalData.nickName && app.globalData.avatarUrl) {
			this.setData({
				nickName: app.globalData.nickName,
				avatarUrl: app.globalData.avatarUrl
			})
		}
		this.uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		Axios.get(app.globalData.baseUrl + 'App/Store/Order/order_list', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			owner: 1,
			status: _this.data.culum,
			store_id: _this.data.store_id,
			p: 1
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					orders: data
				})
				_this.load.loadIng()
			} else {
				_this.showModal(res)
			}
		})
		Axios.get(app.globalData.baseUrl + 'App/PublicCommon/Store/shipper', {}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				var temp = this.data.express;
				var tempid = this.data.expressId;
				data.forEach(function(item) {
					temp.push(item.title)
					tempid.push(item.id)
				})
				_this.setData({
					express: temp,
					expressId: tempid,
					selectExp: this.data.expressId[0]
				})
			} else {
				_this.showModal(res)
			}
		})
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '订单管理'
		})
	},
	preventTouchMove: function() {

	},
	bubbling: function() {

	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	onReachBottom: function() {
		var _this = this;
		wx.showLoading({
			title: '玩命加载中',
		})
		var _this = this
		if (switch_p) {
			switch_p = false
			page = page + 1;
			console.log(page)
			Axios.get(app.globalData.baseUrl + 'App/Store/Order/order_list?p=' + page, {
				uid: this.uid,
				time: this.timesp,
				sign: this.Md5,
				owner: 1,
				status: _this.data.culum,
				store_id: _this.data.store_id,
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					var temp = _this.data.orders.concat(data)
					_this.setData({
						orders: temp
					})
					wx.hideLoading();
					switch_p = true
				} else {
					_this.showModal(res)
				}
			})
		}
	},
	handleCloseTap: function() {
		this.setData({
			priceTk: false,
			cancelTk: false,
			cancelTap: true,
			cancelRepTk: false,
			tips: false,
			SendTk: false,
			agreeTk: false
		})
	},
	handleEvtap: function(e) {
		var _this = this
		_this.setData({
			orders: [],
		})
		var id = e.currentTarget.dataset.id;
		_this.setData({
			cur_id: id,
			culum: id
		})
		_this.load.showIng()
		Axios.get(app.globalData.baseUrl + 'App/Store/Order/order_list', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			owner: 1,
			status: _this.data.cur_id,
			store_id: _this.data.store_id,
			p: 1
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					orders: data,
					cur_id: id,
					culum: id
				})
				_this.load.loadIng()
			} else {
				_this.showModal(res)
			}
		})

	},
	handlePriceTap: function(e) { //修改价格
		var index = e.currentTarget.dataset.index;
		var temp = this.data.orders[index].total_price
		this.setData({
			priceTk: true,
			cur_price: temp,
			cur_pId: index
		})
	},
	bindPriceTap: function(e) { //input绑定
		var value = e.detail.value;
		this.setData({
			cur_price: value
		})
	},
	handleSpTap: function(e) { //确认价格
		var _this = this
		var index = e.currentTarget.dataset.index;
		var price = this.data.cur_price;
		var order_id = this.data.orders[index].id
		if (price != '' && price) {
			Axios.get(app.globalData.baseUrl + 'App/Store/Order/edit_order_price', {
				uid: this.uid,
				time: this.timesp,
				sign: this.Md5,
				owner: 1,
				order_id: order_id,
				store_id: _this.data.store_id,
				price: price
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					this.setData({
						priceTk: false,
						['orders[' + index + '].total_price']: price
					})
				} else {
					_this.showModal(res)
				}
			})
		}
	},
	handleCancelOrder: function(e) { //取消订单弹窗
		var index = e.currentTarget.dataset.index;
		var type = e.currentTarget.dataset.type;
		if (type == 0) {
			this.setData({
				cancelTk: true,
				cur_pId: index
			})
		} else {
			this.setData({
				agreeTk: true,
				cur_pId: index
			})
		}

	},
	handleCancelSure: function(e) { //取消订单确认
		var _this = this
		var index = e.currentTarget.dataset.index;
		var id = this.data.orders[index].id
		Axios.get(app.globalData.baseUrl + 'App/Store/Order/cancel_order', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			owner: 1,
			order_id: id,
			store_id: _this.data.store_id
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				var temp = this.data.orders;
				temp.splice(index, 1);
				_this.setData({
					orders: temp,
					tips: true
				})
			} else {
				_this.showModal(res)
			}
		})
	},
	handleSureOrder: function(e) { //订单确认成功
		var _this = this
		var index = e.currentTarget.dataset.index;
		var id = this.data.orders[index].id
		Axios.get(app.globalData.baseUrl + 'App/Store/Order/commit_order', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			owner: 1,
			order_id: id,
			store_id: _this.data.store_id
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				var temp = this.data.orders;
				temp.splice(index, 1);
				_this.setData({
					orders: temp,
					tips: true
				})
			} else {
				_this.showModal(res)
			}
		})
	},
	handleCancelOrderRep: function(e) { //申请取消订单弹窗
		var index = e.currentTarget.dataset.index;
		this.setData({
			cancelRepTk: true,
			cur_pId: index
		})
	},
	bindCanscelReason: function(e) {
		var value = e.detail.value;
		this.setData({
			Reason: value
		})
	},
	handleRepCancel: function(e) { //取消订单申请
		var _this = this
		var value = this.data.Reason
		var index = e.currentTarget.dataset.index;
		var id = this.data.orders[index].id
		if (value == '') {
			wx.showToast({
				title: '内容不能为空',
				image: '/img/warn-icon.png',
				duration: 1000,
				mask: true
			})
		} else {
			Axios.get(app.globalData.baseUrl + 'App/Store/Order/order_refund', {
				uid: this.uid,
				time: this.timesp,
				sign: this.Md5,
				owner: 1,
				order_id: id,
				store_id: _this.data.store_id,
				refund_reason: _this.data.Reason
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					var temp = this.data.orders;
					temp.splice(index, 1);
					_this.setData({
						orders: temp,
						tips: true
					})
				} else {
					_this.showModal(res)
				}
			})
		}
	},
	handleSendGoodTk: function(e) { //发货弹窗
		var index = e.currentTarget.dataset.index;
		this.setData({
			SendTk: true,
			cur_pId: index
		})
	},
	bindPickerChange: function(e) { //下拉选项
		console.log(e)
		this.setData({
			index: e.detail.value
		})
		this.setData({
			selectExp: this.data.expressId[this.data.index]
		})
	},
	handleScanTap: function() { //扫一扫
		wx.scanCode({
			success: (res) => {
				console.log("扫码结果");
				console.log(res);
				this.setData({
					expressNum: res.result
				})
			},
			fail: (res) => {
				console.log(res);
			}
		})
	},
	handleExpressTapTk: function(e) { //查看物流
		var index = e.currentTarget.dataset.index;
		var id = this.data.orders[index].id;
		wx.navigateTo({
			url:'/pages/logistics/logistics?id='+id
		})
	},
	handleCommentTap: function(e) {
		var index = e.currentTarget.dataset.index
		var id = this.data.orders[index].id
		var store_id = this.data.orders[index].store_id
		wx.navigateTo({
			url: '/pages/shopOrder/evaluate/evaluate?order_id=' + id + '&store_id=' + store_id
		})
	},
	submit: function(e) {
		console.log(e.detail)
		var params = e.detail.value
		if (!this.WxValidate.checkForm(params)) {
			const error = this.WxValidate.errorList[0]
			this.showModal(error)
			return false
		}
		this.submitInfo(params);
	},
	submitInfo: function(e) {
		var _this = this;
		var index = this.data.cur_pId;
		var id = this.data.orders[index].id
		var CONST = {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			order_id: id,
			owner: 1,
			store_id: _this.data.store_id
		}
		var obj = Object.assign(e, CONST);
		Axios.get(app.globalData.baseUrl + 'App/Store/Order/send_order',
			obj, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					var temp = this.data.orders;
					temp.splice(index, 1);
					_this.setData({
						orders: temp,
						tips: true
					})
				} else {
					_this.showModal(res)
				}
			})

	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	initValidate: function() {
		/** 4-2(配置规则)*/
		const rules = {
			courier_id: {
				required: true,
			},
			logistic_code: {
				required: true,
			},

		}
		// 验证字段的提示信息，若不传则调用默认的信息
		const messages = {
			courier_id: {
				required: '选择快递',
			},
			logistic_code: {
				required: '填写订单号',
			}
		}
		// 创建实例对象
		this.WxValidate = new WxValidate.WxValidate(rules, messages)
		/**
		 * 也可以自定义验证规则
		 */
		// 		this.WxValidate.addMethod('assistance', (value, param) => {
		// 			return this.WxValidate.optional(value) || (value.length >= 1 && value.length <= 2)
		// 		}, '请勾选 《顺风男服务协议》')
	},
	handlelettertap:function(e){
		let user = wx.getStorageSync('userInfo') || {}
		var id = e.currentTarget.dataset.id;
		if(id == user.identifier){
			wx.showModal({
				content: '不能和自己聊天',
				showCancel: false,
			})
			return false
		}		
		wx.navigateTo({
			url:'/pages/center/chatRoom/chatRoom?id='+id
		})
	}
})
